package com.mingqi.enetconnected.primary.mytask.adapter;

import android.support.annotation.Nullable;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.mingqi.enetconnected.R;
import com.mingqi.enetconnected.data.bean.order.GoodsOrder;
import com.mingqi.enetconnected.util.AmountTranslateUtil;

import java.util.List;

/**
 * 任务详情-服务内容-耗材信息
 *
 * @author yaxi
 * @date 2018/9/14
 */
public class GoodsInfoAdapter extends BaseQuickAdapter<GoodsOrder, BaseViewHolder> implements BaseQuickAdapter.OnItemLongClickListener, BaseQuickAdapter.OnItemChildClickListener {


    private OnGoodsEditClickedListener mListener;

    public GoodsInfoAdapter(@Nullable List<GoodsOrder> data,OnGoodsEditClickedListener listener) {
        super(R.layout.item_goods_info, data);
        this.mListener = listener;
        setOnItemLongClickListener(this);
        setOnItemChildClickListener(this);
    }

    @Override
    protected void convert(BaseViewHolder helper, GoodsOrder item) {
        helper.addOnClickListener(R.id.goods_item_delete)
                .addOnClickListener(R.id.iv_amount_edit);
        helper.setGone(R.id.goods_item_delete_container, item.isShowDeleteButton());

        String productName = item.getProductName();
        String priceStr = AmountTranslateUtil.decorate(AmountTranslateUtil.transformAmount(item.getPrice()));
        String paidFeeStr = AmountTranslateUtil.decorate(AmountTranslateUtil.transformAmount(item.getPaidFee()));
        if (productName != null) {
            helper.setText(R.id.tv_goods_info_name, item.getProductName());
        }
        helper.setText(R.id.tv_goods_unit_price, paidFeeStr);
        helper.setText(R.id.tv_goods_total_price, priceStr + "x" + item.getNum());
    }


    @Override
    public boolean onItemLongClick(BaseQuickAdapter adapter, View view, int position) {
        ((GoodsOrder) adapter.getItem(position)).setShowDeleteButton(true);
        refreshNotifyItemChanged(position);
        return true;
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        switch (view.getId()) {
            case R.id.goods_item_delete:
                ((GoodsOrder) adapter.getItem(position)).setShowDeleteButton(false);
                remove(position);
                break;
            case R.id.goods_item_delete_container:
                ((GoodsOrder) adapter.getItem(position)).setShowDeleteButton(false);
                refreshNotifyItemChanged(position);
                break;
            case R.id.iv_amount_edit:
                if (mListener != null){
                    mListener.onGoodsEdit(position,GoodsInfoAdapter.this);
                }
                break;
            default:
                break;
        }
    }


    public interface OnGoodsEditClickedListener {
        void onGoodsEdit(int position,GoodsInfoAdapter adapter);
    }
}
